package dao;

import entity.Product;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;

public class WishListDao {
    private static PreparedStatement statement = null;
    private static ResultSet result = null;


    /**
     * 得到用户收藏的商品列表
     * @param userId 用户ID
     * @return
     * @throws SQLException
     */
    public static Map<String , List<Product>> getUserWishList(int userId) throws SQLException {
        int key = 0,flag = 0;
        Map<String,List<Product>> wishList = new HashMap<>();
        String sql = "SELECT * FROM product p INNER JOIN wishlist w ON w.product_id = p.product_id " +
                " WHERE w.user_id = " + userId + " ORDER BY category_id ASC";
        statement = DataBaseOperation.getConnectionAndStatement(sql,statement);
        result = statement.executeQuery();
        List<Product> productList = new ArrayList<>();
        while(result.next()){
            flag = 1;
            if(result.getInt("category_id") != key && key != 0) {
                flag = 0;
                wishList.put(CategoryDao.getCategoryName(key),productList);
                productList = new ArrayList<>();
            }
            productList.add(new Product(result.getInt("product_id"),result.getString("product_name"),result.getInt("category_id"),
                    result.getDouble("price"),result.getString("note"),result.getString("default_img"),
                    result.getInt("hot_id"),result.getInt("review_id"),result.getInt("store")));
            key = result.getInt("category_id");
        }
        if(flag != 0) wishList.put(CategoryDao.getCategoryName(key),productList);
        DataBaseOperation.close(result,statement);
        return  wishList;
    }

    /**
     * 添加收藏
     * @param productId 商品ID
     * @param userId 用户ID
     * @return
     * @throws SQLException
     */
    public static String addProductWish(String productId,String userId) throws SQLException {
        String sql = "SELECT * FROM wishlist WHERE product_id = " + productId + " AND user_id = " + userId;
        statement = DataBaseOperation.getConnectionAndStatement(sql,statement);
        result = statement.executeQuery();
        if(result.next()){
            return "product repeat";
        }
        sql = "INSERT INTO wishlist(user_id,product_id) VALUES(" + Integer.parseInt(userId) + "," + Integer.parseInt(productId) + ")";
        statement = DataBaseOperation.getConnectionAndStatement(sql,statement);
        int result = statement.executeUpdate();
        DataBaseOperation.close(statement);
        return result > 0 ? "Success" : "Error";
    }

    /**
     * 删除商品
     * @param productId 商品ID
     * @return
     */
    public static int deleteProduct(String productId,String userId) {
        String sql = "DELETE FROM wishlist WHERE product_id = " + productId + " AND user_id = " + userId;
        int result = 0;
        try {
            statement = DataBaseOperation.getConnectionAndStatement(sql,statement);
            result = statement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        DataBaseOperation.close(statement);
        return result;
    }
}
